< previous page page_407 next page >

Page 407
Although this appears to the outside world as a property of the class, the class itself does not hold a private variable about the file being too big. The Property Get procedure simply calculates this every time its called and returns a True or False value. This property could just as easily have been implemented as a method of the class, but the point was to show you how to use a property for something other than just retrieving private data from the class.
9f31265bb40b592b1929e18a47ab28e5.gif
TIP
Property Get and Property Let procedures can perform any Visual Basic code, just like any function and sub.
The Class_Terminate Event
Whenever an object goes out of scope, a destructor function for that object is called. In Visual Basic, this is called the Class_Terminate() event. This procedure fires when an object is either set equal to Nothing or goes out of scope (as is the case with a local object).
9f31265bb40b592b1929e18a47ab28e5.gif
NOTE
Clicking the End button or terminating your application with the
End statement does not fire the Terminate event for any user-defined objects.
The Class_Terminate() event is the destructor in Visual Basic thats called when an object is destroyed. In the sample TextStream class, a file handle is opened when the class is first instantiated. To ensure that this file handle gets closed, you should call the CloseFile() method. However, programmers sometimes will get lazy and forget to call the CloseFile() method. In the Class_Terminate() event of the TextStream class, you can call the CloseFile() method for them to ensure that it does get called.
Heres the code for the Class_Terminate() event in the TextStream class:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub Class_Terminate()
   Dim boolRet As Boolean

   boolRet = CloseFile()
End Sub
In this event, the CloseFile() method is called to make sure that if the file handle is open, it is closed. A class can always call one of its own methods, instead of having to duplicate code from one method to another.

 
< previous page page_407 next page >

If you like this book, buy it!